﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace DataAccess
{
    public class OrderReposity : ConnectionClass
    {
         public OrderReposity() : base(){}

        public void AddOrder(Order o)
        {
            db.AddToOrders(o);
            db.SaveChanges();
        }

        public void AddOrderDetail(OrderDetail od)
        {
            db.OrderDetails.AddObject(od);
            db.SaveChanges();
        }

        public Order GetOrderByID(Guid ID)
        {
            return db.Orders.SingleOrDefault(o => o.ID == ID);
        }


        public IEnumerable<OrderDetail> GettAllOrdersDetails()
        {
            return db.OrderDetails;
        }

        public OrderDetail GetOrderDetailByID(int id)
        {
            return db.OrderDetails.SingleOrDefault(x => x.ID == id);
        }

        public IEnumerable<OrderDetail> GetOrderDetailByOrderID(Guid id)
        {
            return db.OrderDetails.Where(x => x.OrderID == id);
        }

        public void UpdateOrderDetail(OrderDetail od)
        {
            OrderDetail O = GetOrderDetailByID(od.ID);
            db.OrderDetails.Attach(O);
            db.OrderDetails.ApplyCurrentValues(od);
            db.SaveChanges();
        }

        public IEnumerable<Order> GetAllOrderByUserName(string username)
        {
            return db.Orders.Where(x => x.UserName == username);
        }


    }
}
